package com.block.merchant.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.ImUserBaseInfo;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.MerchantDetailInfo;
import com.block.db.entity.MerchantUserInfo;
import com.block.db.mapper.MerchantDetailInfoMapper;
import com.block.db.mapper.MerchantServeCityInfoMapper;
import com.block.im.enums.UserTypeEnum;
import com.block.im.model.param.ImUserParam;
import com.block.im.service.IImUserBaseInfoService;
import com.block.merchant.model.vo.MerchantInfoVO;
import com.block.merchant.model.vo.PersonInfoVO;
import com.block.merchant.service.IMerchantBaseInfoService;
import com.block.merchant.service.IMerchantDetailInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.merchant.service.IMerchantUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商户详情信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-25
 */
@Service
public class MerchantDetailInfoServiceImpl extends ServiceImpl<MerchantDetailInfoMapper, MerchantDetailInfo> implements IMerchantDetailInfoService {

    @Resource
    MerchantServeCityInfoMapper merchantServeCityInfoMapper;
    @Autowired
    IMerchantBaseInfoService merchantBaseInfoService;
    @Autowired
    IMerchantUserInfoService merchantUserInfoService;
    @Autowired
    IImUserBaseInfoService userBaseInfoService;

    @Override
    public ResponseVo getPageDataList(String cityCode) {

        // 1. 根据地址信息 查询满足条件的 商户信息
        List<Integer> infoIdList = merchantServeCityInfoMapper.getInfoIdListByCityCode(cityCode);

        List<MerchantInfoVO> dataList = new ArrayList<>();
        if (CollUtil.isNotEmpty(infoIdList)){
            for (Integer infoId : infoIdList) {
                MerchantDetailInfo detailInfo = this.baseMapper.selectById(infoId);

                MerchantInfoVO merchantInfoVO = new MerchantInfoVO();
                merchantInfoVO.setInfoId(infoId);
                merchantInfoVO.setProductLogo(detailInfo.getProductLogo());
                merchantInfoVO.setProductName(detailInfo.getProductName());
                merchantInfoVO.setCompanyName(detailInfo.getCompanyName());
                merchantInfoVO.setLabelInfo(detailInfo.getLabelInfo());
                merchantInfoVO.setAddressInfo(detailInfo.getAddressInfo());

                dataList.add(merchantInfoVO);
            }
        }

        JSONObject returnData = new JSONObject();
        returnData.put("dataList", dataList);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, returnData);
    }

    @Override
    public ResponseVo getDetailInfoByInfoId(String infoId) {

        MerchantDetailInfo merchantDetailInfo = this.getById(Integer.valueOf(infoId));

        MerchantInfoVO merchantInfoVO = new MerchantInfoVO();
        merchantInfoVO.setInfoId(merchantDetailInfo.getInfoId());
        merchantInfoVO.setProductLogo(merchantDetailInfo.getProductLogo());
        merchantInfoVO.setProductName(merchantDetailInfo.getProductName());
        merchantInfoVO.setCompanyName(merchantDetailInfo.getCompanyName());
        merchantInfoVO.setLabelInfo(merchantDetailInfo.getLabelInfo());
        merchantInfoVO.setAddressInfo(merchantDetailInfo.getAddressInfo());
        merchantInfoVO.setIntroInfo(merchantDetailInfo.getIntroInfo());
//        merchantInfoVO.setPositionInfo(merchantDetailInfo.getPositionInfo());
        merchantInfoVO.setPositionInfo(merchantDetailInfo.getAddressInfo());

        // 查询机构基本信息
        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.getById(infoId);

        MerchantUserInfo canPushUserInfo = merchantUserInfoService.getCanPushUserInfo(Integer.valueOf(infoId));

        // todo 查询客服人员信息
        PersonInfoVO personInfoVO = new PersonInfoVO();
        personInfoVO.setLastReply("1小时前");
        personInfoVO.setRepayNumber("20+");
        personInfoVO.setUserImage(merchantDetailInfo.getProductLogo());

        ImUserParam imUserParam = null;
        if (ObjectUtil.isEmpty(canPushUserInfo)){
            // 业务员都没有在线
            personInfoVO.setPostName("客服主管");
            imUserParam = new ImUserParam(merchantBaseInfo.getAdminUserid(), UserTypeEnum.BUS);

            imUserParam.setNickname(merchantBaseInfo.getCompanyShortName());
            imUserParam.setHeadImage(merchantDetailInfo.getProductLogo());
        }else{
            // 显示管理账号
            personInfoVO.setPostName("客服经理");
            imUserParam = new ImUserParam(String.valueOf(canPushUserInfo.getUid()), UserTypeEnum.BUS);

            imUserParam.setNickname(canPushUserInfo.getNickName());
            imUserParam.setHeadImage(merchantDetailInfo.getProductLogo());
        }

        ImUserBaseInfo imUserInfo = userBaseInfoService.getImUserInfo(imUserParam);
        if (ObjectUtil.isNotEmpty(imUserInfo)){
            personInfoVO.setUserId(imUserInfo.getUserId());
            personInfoVO.setUsername(imUserInfo.getNickname());
        }else{
            // TODO: 2024/10/15 报警
        }

        JSONObject returnData = new JSONObject();
        returnData.put("merchantInfo", merchantInfoVO);
        returnData.put("personInfo", personInfoVO);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, returnData);
    }
}
